package com.ruoyi.basicData.controller;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;


import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basicData.domain.CustomerList;
import com.ruoyi.basicData.service.ICustomerListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 客户管理Controller
 * 
 * @author admin
 * @date 2024-06-27
 */

@RestController
@RequestMapping("/basicData/customerList")
public class CustomerListController extends BaseController
{
    @Autowired
    private ICustomerListService customerListService;

    /**
     * 查询客户管理列表
     */

    @PreAuthorize("@ss.hasPermi('basicData:customerList:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerList customerList)
    {
        startPage();
        List<CustomerList> list = customerListService.selectCustomerListList(customerList);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicData:customerList:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerList customerList)
    {
        List<CustomerList> list = customerListService.selectCustomerListList(customerList);
        ExcelUtil<CustomerList> util = new ExcelUtil<CustomerList>(CustomerList.class);
        util.exportExcel(response, list, "客户管理数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CustomerList> util = new ExcelUtil<CustomerList>(CustomerList.class);
        util.importTemplateExcel(response, "客户管理数据");
    }
    /**
     * 导入数据
     */
    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basicData:list:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<CustomerList> util = new ExcelUtil<CustomerList>(CustomerList.class);
        InputStream inputStream = file.getInputStream();
        List<CustomerList> list = util.importExcel(inputStream );
        inputStream.close();
        int count = customerListService.batchInsertCustomerList(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }


    /**
     * 获取客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicData:customerList:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return success(customerListService.selectCustomerListByCustomerId(customerId));
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:customerList:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerList customerList)
    {
        return toAjax(customerListService.insertCustomerList(customerList));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:customerList:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerList customerList)
    {
        return toAjax(customerListService.updateCustomerList(customerList));
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('basicData:customerList:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds)
    {
        return toAjax(customerListService.deleteCustomerListByCustomerIds(customerIds));
    }
}
